
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import javax.swing.JComboBox;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class BusStopsInArea
{}


/*
 * PassengerInterface.java
 *
 * Created on 26-Apr-2012, 14:15:32
 */

/**
 *
 * @author matrosj0
 */
public class PassengerInterface extends javax.swing.JFrame {

    RouteGenerator routeGen;

    String[] busStopNames;
    String[] areaNames;

    String startLocation;
    String startLocationArea;
    String endLocation;
    String endLocationArea;
    String startArea;
    String endArea;

    // fills in an array busStopNames with right bust stops in format "bus stop, area"
    // this array will be used to print all items in the drop-list menu
    private void busStopsToArray()
    {
        ArrayList<Location> locations = new ArrayList<Location> (routeGen.getLocationsMap().values());

        busStopNames = new String[locations.size()];
        for (int i = 0; i< busStopNames.length; i++)
        {
            busStopNames[i] = locations.get(i).getStopName() + ", " + locations.get(i).getAreaName();
        }
        java.util.Arrays.sort(busStopNames);
    }

    // fills in an array areaNames, that will be used to print all items in the
    // drop list menu
    private void areasToArray()
    {
        HashSet<String> areas = new HashSet<String>();

        for (int i = 0; i < this.busStopNames.length; i++)
        {
            String[] s = this.busStopNames[i].split(", ");
            areas.add(s[1]);
        }

        ArrayList<String> areasAL = new ArrayList<String>(areas);
        areaNames = new String[areasAL.size()];
        areasAL.toArray(areaNames);
        java.util.Arrays.sort(areaNames);
    }

    /** Creates new form PassengerInterface */
    public PassengerInterface() 
    {
        routeGen = new RouteGenerator();

        busStopsToArray();

        areasToArray();

        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        startLocationComboBox = new javax.swing.JComboBox();
        endLocationComboBox = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        generateButtonLocation = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        startAreaComboBox = new javax.swing.JComboBox();
        endAreaComboBox = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        generateButtonArea = new javax.swing.JButton();
        jLabel7 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        outputArea = new javax.swing.JTextArea();
        jLabel8 = new javax.swing.JLabel();
        dayBox = new javax.swing.JComboBox();
        jLabel9 = new javax.swing.JLabel();
        hourTextField = new javax.swing.JFormattedTextField(new java.text.SimpleDateFormat("HH"));
        minuteTextField = new javax.swing.JFormattedTextField(new java.text.SimpleDateFormat("mm"));

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);

        startLocationComboBox.setModel(new javax.swing.DefaultComboBoxModel(this.busStopNames));
        startLocationComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                startLocationComboBoxActionPerformed(evt);
            }
        });

        endLocationComboBox.setModel(new javax.swing.DefaultComboBoxModel(this.busStopNames));
        endLocationComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                endLocationComboBoxActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("DejaVu Sans", 1, 13));
        jLabel1.setText("Choose Locations: ");

        jLabel2.setFont(new java.awt.Font("DejaVu Sans", 2, 18));
        jLabel2.setText("or");

        generateButtonLocation.setText("Generate Best Route");
        generateButtonLocation.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                generateButtonLocationActionPerformed(evt);
            }
        });

        jLabel3.setFont(new java.awt.Font("DejaVu Sans", 1, 13));
        jLabel3.setText("Choose Areas: ");

        startAreaComboBox.setModel(new javax.swing.DefaultComboBoxModel(areaNames));
        startAreaComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                startAreaComboBoxActionPerformed(evt);
            }
        });

        endAreaComboBox.setModel(new javax.swing.DefaultComboBoxModel(areaNames));
        endAreaComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                endAreaComboBoxActionPerformed(evt);
            }
        });

        jLabel4.setText("to");

        jLabel5.setText("to");

        jLabel6.setFont(new java.awt.Font("DejaVu Sans", 1, 24));
        jLabel6.setText("Passenger Interface");

        generateButtonArea.setText("Generate Best Route");
        generateButtonArea.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                generateButtonAreaActionPerformed(evt);
            }
        });

        jLabel7.setText("____________________________________________________________________________");

        outputArea.setColumns(20);
        outputArea.setRows(5);
        outputArea.setDisabledTextColor(new java.awt.Color(1, 1, 1));
        outputArea.setEnabled(false);
        jScrollPane1.setViewportView(outputArea);

        jLabel8.setFont(new java.awt.Font("DejaVu Sans", 0, 14));
        jLabel8.setText("Chose start time");

        dayBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" }));

        jLabel9.setFont(new java.awt.Font("DejaVu Sans", 1, 18));
        jLabel9.setText(":");

        hourTextField.setText("12");

        minuteTextField.setText("00");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 569, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGap(23, 23, 23)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel3)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(startAreaComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel5)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(endAreaComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel8, javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel6))
                                .addGap(18, 18, 18)
                                .addComponent(hourTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel9)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(minuteTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGap(246, 246, 246)
                        .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGap(31, 31, 31)
                        .addComponent(jLabel7, javax.swing.GroupLayout.DEFAULT_SIZE, 550, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap(435, Short.MAX_VALUE)
                        .addComponent(generateButtonArea))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(14, 14, 14)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 421, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(generateButtonLocation))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jLabel4)
                                        .addGap(12, 12, 12))
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jLabel1)
                                        .addGap(18, 18, 18)))
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(endLocationComboBox, 0, 404, Short.MAX_VALUE)
                                    .addComponent(startLocationComboBox, 0, 404, Short.MAX_VALUE)
                                    .addComponent(dayBox, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE))))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel6)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(dayBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(hourTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel9)
                    .addComponent(minuteTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(startLocationComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(endLocationComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(generateButtonLocation)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel2)
                .addGap(4, 4, 4)
                .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(startAreaComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(endAreaComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5))
                .addGap(18, 18, 18)
                .addComponent(generateButtonArea)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 292, Short.MAX_VALUE)
                .addContainerGap())
        );

        // set startLocation to the first item in the list
        String[] s = busStopNames[0].split(", ");
        startLocation = s[0];
        startLocationArea = s[1];
        // set end location
        s = busStopNames[0].split(", ");
        endLocation = s[0];
        endLocationArea = s[1];
        startArea = areaNames[0];
        endArea = areaNames[0];

        pack();
    }// </editor-fold>//GEN-END:initComponents

     private int getDayInt()
    {
        String enteredDay = (String) this.dayBox.getSelectedItem();

        if (enteredDay.equalsIgnoreCase("mon"))
            return 0;
        else if(enteredDay.equalsIgnoreCase("tue"))
            return 1;
        else if(enteredDay.equalsIgnoreCase("wed"))
            return 2;
        else if(enteredDay.equalsIgnoreCase("thu"))
            return 3;
        else if(enteredDay.equalsIgnoreCase("fri"))
            return 4;
        else if(enteredDay.equalsIgnoreCase("sat"))
            return 5;
        else if(enteredDay.equalsIgnoreCase("sun"))
            return 6;
        else
            return -1;

    }

    private void generateButtonLocationActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_generateButtonLocationActionPerformed
        Location start = routeGen.getLocationInGraph(startLocation, startLocationArea);
        Location end = routeGen.getLocationInGraph(endLocation, endLocationArea);

        //System.out.println(endLocation + ", " + endLocationArea);
        //System.out.println("from" + start + "\n\nto" + end);

        int day = getDayInt();
        int hours = Integer.parseInt(hourTextField.getText());
        int mins = Integer.parseInt(minuteTextField.getText());
        Calendar enteredDate = Calendar.getInstance();
        enteredDate.set(Calendar.DAY_OF_WEEK, day + 2);
        enteredDate.set(Calendar.HOUR_OF_DAY, hours);
        enteredDate.set(Calendar.MINUTE, mins);
        routeGen.setStartTime(enteredDate);
        ArrayList<Location> route = routeGen.generateBestRoute(start, end);

        outputArea.setText("");

        if (route.size() == 1)
            outputArea.append("We couldn't find a route from your current location to your current location");
        else
        {
            // print the starting location
            outputArea.append("You a going from:\n");
            outputArea.append(route.get(0).getStopName() + " in " + route.get(0).getAreaName());
            if (route.get(0).getLatestTime() != null)
                outputArea.append("\n@ " + Service.calendarToString(route.get(0).getLatestTime()));

            for (int i = 1; i < route.size() - 1; i++)
            {
                outputArea.append("\n\nVia\n");
                Location l = route.get(i);
                outputArea.append(l.getStopName() + " in " + l.getAreaName());
                if (l.getPreviousService() != null)
                    outputArea.append("\nService: " + Integer.toString(l.getPreviousService().getServiceID())
                            + "(Route: " + Integer.toString(l.getPreviousService().getRoute().getRouteID()) + ")");

                if (l.getLatestTime() != null)
                    outputArea.append("\n@ " + Service.calendarToString(l.getLatestTime()));
            }

            // print the destination
            outputArea.append("\n\nTo\n" + route.get(route.size() - 1).getStopName() + " in "
                                + route.get(route.size() - 1).getAreaName());
            if (route.get(route.size() - 1).getPreviousService() != null)
                outputArea.append("\nService: " + Integer.toString(route.get(route.size() - 1).getPreviousService().getServiceID())
                            + "(Route: " + Integer.toString(route.get(route.size() - 1).getPreviousService().getRoute().getRouteID()) + ")");
            if (route.get(0).getLatestTime() != null)
                outputArea.append("\n@ " + Service.calendarToString(route.get(route.size() - 1).getLatestTime()));
        }

    }//GEN-LAST:event_generateButtonLocationActionPerformed

    private void startLocationComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_startLocationComboBoxActionPerformed
        String[] s = ((String) startLocationComboBox.getSelectedItem()).split(", ");
        startLocation = s[0];
        startLocationArea = s[1];
    }//GEN-LAST:event_startLocationComboBoxActionPerformed

    private void endLocationComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_endLocationComboBoxActionPerformed
        String[] s  = ((String) endLocationComboBox.getSelectedItem()).split(", ");
        endLocation = s[0];
        endLocationArea = s[1];
    }//GEN-LAST:event_endLocationComboBoxActionPerformed

    private void startAreaComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_startAreaComboBoxActionPerformed
        startArea = (String) this.startAreaComboBox.getSelectedItem();
    }//GEN-LAST:event_startAreaComboBoxActionPerformed

    private void endAreaComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_endAreaComboBoxActionPerformed
        endArea = (String) this.endAreaComboBox.getSelectedItem();
    }//GEN-LAST:event_endAreaComboBoxActionPerformed

    private void generateButtonAreaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_generateButtonAreaActionPerformed
        System.out.println("from " + startArea + " to " + endArea);

        int day = getDayInt();
        int hours = Integer.parseInt(hourTextField.getText());
        int mins = Integer.parseInt(minuteTextField.getText());
        Calendar enteredDate = Calendar.getInstance();
        enteredDate.set(Calendar.DAY_OF_WEEK, day + 2);
        enteredDate.set(Calendar.HOUR_OF_DAY, hours);
        enteredDate.set(Calendar.MINUTE, mins);
        routeGen.setStartTime(enteredDate);
        
        ArrayList<Location> route = routeGen.getBestRouteBetweenAreas(startArea, endArea);

        outputArea.setText("");

        if (route == null)
            outputArea.setText("No route found, sorry");
        else if (route.size() == 1)
            outputArea.setText("We couldn't find a route from your current location to your current location");
        else
        {
            // display the starting location
            outputArea.append("You a going from:\n");
            outputArea.append(route.get(0).getStopName() + " in " + route.get(0).getAreaName());
            if (route.get(0).getLatestTime() != null)
                outputArea.append("\n@ " + Service.calendarToString(route.get(0).getLatestTime()));
            
            for (int i = 1; i < route.size() - 1; i++)
            {
                outputArea.append("\n\nVia\n");
                Location l = route.get(i);
                outputArea.append(l.getStopName() + " in " + l.getAreaName() + "\n");
                if (l.getPreviousService() != null)
                    outputArea.append("Service: " + Integer.toString(l.getPreviousService().getServiceID())
                            + "(Route: " + Integer.toString(l.getPreviousService().getRoute().getRouteID()) + ")");

                if (l.getLatestTime() != null)
                    outputArea.append("\n@ " + Service.calendarToString(l.getLatestTime()));
            }
            
            // print the destination
            outputArea.append("\n\nTo\n" + route.get(route.size() - 1).getStopName() + " in "
                                + route.get(route.size() - 1).getAreaName());
            if (route.get(route.size() - 1).getPreviousService() != null)
                outputArea.append("\nService: " + Integer.toString(route.get(route.size() - 1).getPreviousService().getServiceID())
                            + "(Route: " + Integer.toString(route.get(route.size() - 1).getPreviousService().getRoute().getRouteID()) + ")");
            if (route.get(0).getLatestTime() != null)
                outputArea.append("\n@ " + Service.calendarToString(route.get(route.size() - 1).getLatestTime()));
        }
    }//GEN-LAST:event_generateButtonAreaActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        database.openBusDatabase();
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new PassengerInterface().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox dayBox;
    private javax.swing.JComboBox endAreaComboBox;
    private javax.swing.JComboBox endLocationComboBox;
    private javax.swing.JButton generateButtonArea;
    private javax.swing.JButton generateButtonLocation;
    private javax.swing.JFormattedTextField hourTextField;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JFormattedTextField minuteTextField;
    private javax.swing.JTextArea outputArea;
    private javax.swing.JComboBox startAreaComboBox;
    private javax.swing.JComboBox startLocationComboBox;
    // End of variables declaration//GEN-END:variables

}
